import 'package:flutter/material.dart';
import 'package:city_pickers/city_pickers.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
//import 'package:flutterapp6/map/addresslist.dart';
import '../map/addresslist.dart';
import '../model/cartmodellist.dart';
import '../pages/member.dart';
class Address extends StatefulWidget {
  Address({Key key}) : super(key: key);

  @override
  _AddressState createState() => _AddressState();
}

class _AddressState extends State<Address> {
  List addresslist;
  @override
 void initState() {
  super.initState();
  Addressl().getAddress();
 //print('我是地址$addresList');
 
  list();
 
} 
void list()async{
  Addressl().getAddress();
   Addressl().getAddress();
   setState(() {
    addresslist=addresList==null?[]:addresList;
  });
   print('我是地址$addresslist');
}
//渲染收货人地址区域
    Widget _address(BuildContext context,int index){
       final ValueNotifier<List> _counter = ValueNotifier<List>([]);
      return
       ValueListenableBuilder( child: null, valueListenable: _counter, builder: (BuildContext context,value,Widget child){
         return Row(
        crossAxisAlignment: CrossAxisAlignment.center,
       // mainAxisAlignment: MainAxisAlignment.center,
        children: <Widget>[
         Container(
           padding: EdgeInsets.all(2),
           width: ScreenUtil().setHeight(80),
           height: ScreenUtil().setWidth(80),
           child: Center(
             child: Text('${addresslist[index]['username']}',style: TextStyle(fontSize: 10,color: Colors.white),maxLines: 1,  overflow: TextOverflow.ellipsis,),
           ),
           decoration: BoxDecoration(
             color: Colors.grey,
             shape: BoxShape.circle
           ),
         ),
        Container(
         // color: Colors.pink,
          width: ScreenUtil().setWidth(500),
          height: ScreenUtil().setHeight(120),
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            crossAxisAlignment: CrossAxisAlignment.start,
            children: <Widget>[
                Row(
                  children: <Widget>[
                     SizedBox(width:10),
                    Text('${addresslist[index]['username']}',style: TextStyle(fontSize:14,color:Colors.black54),),
                    SizedBox(width:10),
                    Text('${addresslist[index]['phone']}',style: TextStyle(fontSize:12,color:Colors.black26),),
                     SizedBox(width:10),
                    Text('${addresslist[index]['flag']?"默认":''}',style: TextStyle(fontSize:10,color:Colors.orange[500]),)
                  ],
                ),
                 SizedBox(width:10),
                Text('${addresslist[index]['addsl']}${addresslist[index]['city']}',maxLines: 2, overflow: TextOverflow.ellipsis,)
            ],
          ),
        ),
        Container(
         height: ScreenUtil().setHeight(80),
         padding: EdgeInsets.all(10),
         decoration: BoxDecoration(
           border: Border(
             left: BorderSide(width:1,color:Colors.grey)
           )
         ),
          child: InkWell(
            onTap: (){
             
             setState(() {
                 Addressl().remove(index);
              // Addressl().getAddress();
               this.list();
               Navigator.of(context).pushReplacementNamed('/address');
               // Navigator.pushNamed(context, '/address');
             });
              print('我点击了删除');
            },
            child: Center(
              
              child: Text('删除'),
            ),
          ),
        )
        ],
      );
       });
    }
  @override
  Widget build(BuildContext context) {
     final ValueNotifier<List> _counter = ValueNotifier<List>([]);
    return Scaffold(
      appBar: AppBar(
        leading: new IconButton(
               icon: new Icon(Icons.arrow_back, color: Colors.white),
               onPressed: () {
                 Navigator.pop(context);
      //             Navigator.of(context).pushAndRemoveUntil(
      // new MaterialPageRoute(
      //     builder: (context) => MemberPage()
      // ), (route) => route == null);
               },
              ),
        title: Container(child:Center(child: Text('地址管理'),)),),
      body: Container(
       child: ValueListenableBuilder(
         child: null,
         valueListenable: _counter, builder: (BuildContext context,value,Widget child){
         return FutureBuilder(builder: (context,snapshot){
           return Column(
         children: <Widget>[
           Container(
             width: ScreenUtil().setWidth(750),
             child: Column(
               children: <Widget>[
                
                 Container(
                   width: ScreenUtil().setWidth(750),
                  height: ScreenUtil().setHeight(addresslist.length*120),
                   child: ListView.builder(itemBuilder: _address,itemCount:addresslist.length)
                 )
               ],
             ),
           ),
         Center(child:  RaisedButton(child: Text('新增地址'), onPressed: (){
         //_clickCity();
         Navigator.pushNamed(context, '/addresslist');
       }),)
         ],
       );
         });
       })
    ),
    );
  }
}
